namespace RivercrabNet
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Drawing;
    using System.Drawing.Drawing2D;

    public class IndicatorDirectionDeviationIndex : BaseIndicator
    {
        private BarShape bsDdi;
        private string dmfField = ("DDI.DmfField" + Common.GetGuid());
        private string dmfSumField = ("DDI.DmfSumField" + Common.GetGuid());
        private string dmzField = ("DDI.DmzField" + Common.GetGuid());
        private string dmzSumField = ("DDI.DmzSumField" + Common.GetGuid());
        private LineShape tlsAd;
        private LineShape tlsAddi;

        public IndicatorDirectionDeviationIndex()
        {
            base.IndType = IndicatorType.DDI;
            base.IndName = "DDI";
            base.IndDesc = "方向标准离差指数";
            base.IndPeriod.Add(new IndicatorPeriod(0, "N", 13.0));
            base.IndPeriod.Add(new IndicatorPeriod(1, "N1", 30.0));
            base.IndPeriod.Add(new IndicatorPeriod(2, "M", 10.0));
            base.IndPeriod.Add(new IndicatorPeriod(3, "M1", 5.0));
            base.SourceDic["High"] = "";
            base.SourceDic["Low"] = "";
            base.fields = new string[] { "DDI", "ADDI", "AD" };
        }

        public override bool Contains(object obj)
        {
            if (((obj != this.bsDdi) && (obj != this.tlsAddi)) && (obj != this.tlsAd))
            {
                return false;
            }
            return true;
        }

        public override double[] Get(int r)
        {
            double curHigh = Convert.ToDouble(base.Crab.DataSource.Rows[r][base.SourceDic["High"]]);
            double curLow = Convert.ToDouble(base.Crab.DataSource.Rows[r][base.SourceDic["Low"]]);
            double num3 = 0.0;
            if (r > 0)
            {
                num3 = Convert.ToDouble(base.Crab.DataSource.Rows[r - 1][base.SourceDic["High"]]);
            }
            double num4 = 0.0;
            if (r > 0)
            {
                num4 = Convert.ToDouble(base.Crab.DataSource.Rows[r - 1][base.SourceDic["Low"]]);
            }
            double num5 = Common.CalculateTrueRange(r, curHigh, curLow, base.SourceDic["High"], base.SourceDic["Low"], base.Crab.DataSource);
            double curValue = 0.0;
            if ((curHigh + curLow) <= (num3 + num4))
            {
                curValue = 0.0;
            }
            else
            {
                curValue = num5;
            }
            double num7 = 0.0;
            if ((curHigh + curLow) >= (num3 + num4))
            {
                num7 = 0.0;
            }
            else
            {
                num7 = num5;
            }
            double num8 = Common.CalcutaSumilationValue(r, this.dmzSumField, (int) base.IndPeriod[0].ParamValue, this.dmzField, curValue, base.Crab.DataSource);
            double num9 = Common.CalcutaSumilationValue(r, this.dmfSumField, (int) base.IndPeriod[0].ParamValue, this.dmfField, num7, base.Crab.DataSource);
            double num10 = ((num8 + num9) != 0.0) ? (num8 / (num8 + num9)) : 0.0;
            double num11 = ((num8 + num9) != 0.0) ? (num9 / (num8 + num9)) : 0.0;
            double num12 = num10 - num11;
            double num13 = Common.CalcuteSimpleMovingAvg(r, (int) base.IndPeriod[1].ParamValue, (int) base.IndPeriod[2].ParamValue, this.tlsAddi.Name, this.bsDdi.Name, num12, base.Crab.DataSource);
            double num14 = Common.CalcuteMovingAvg(r, (int) base.IndPeriod[3].ParamValue, this.tlsAd.Name, this.tlsAddi.Name, num13, base.Crab.DataSource);
            return new double[] { curValue, num7, num8, num9, num12, num13, num14 };
        }

        public override List<BaseShape> GetShapeList()
        {
            return new List<BaseShape> { this.bsDdi, this.tlsAddi, this.tlsAd };
        }

        public override void Remove()
        {
            base.Crab.DeleteShape(this.bsDdi);
            base.Crab.DeleteShape(this.tlsAddi);
            base.Crab.DeleteShape(this.tlsAd);
            base.Crab.DataSource.Columns.Remove(this.dmfField);
            base.Crab.DataSource.Columns.Remove(this.dmzField);
            base.Crab.DataSource.Columns.Remove(this.dmzSumField);
            base.Crab.DataSource.Columns.Remove(this.dmfSumField);
        }

        public override bool Set(int r, string target)
        {
            try
            {
                DataRow row = base.Crab.DataSource.Rows[r];
                foreach (string str in base.SourceDic.Values)
                {
                    if ((str == target) || (target == null))
                    {
                        foreach (string str2 in base.SourceDic.Values)
                        {
                            if (row[str2] is DBNull)
                            {
                                return false;
                            }
                        }
                        double[] numArray = this.Get(r);
                        base.Crab.SetValue(this.dmzField, numArray[0], r);
                        base.Crab.SetValue(this.dmfField, numArray[1], r);
                        base.Crab.SetValue(this.dmzSumField, numArray[2], r);
                        base.Crab.SetValue(this.dmfSumField, numArray[3], r);
                        base.Crab.SetValue(this.bsDdi.Name, numArray[4], r);
                        base.Crab.SetValue(this.tlsAddi.Name, numArray[5], r);
                        base.Crab.SetValue(this.tlsAd.Name, numArray[6], r);
                    }
                }
                return false;
            }
            catch
            {
                return false;
            }
        }

        public override void SetParam(string[] fields, string[] source, double[] periods, int panelID)
        {
            this.BsDdi = base.Crab.AddBar(fields[0], panelID);
            base.Crab.SetBar(fields[0], fields[0], Color.Red, Color.SkyBlue, 1, 0.01, 2, BarStyle.Line);
            this.TlsAddi = base.Crab.AddLine(fields[1], panelID);
            base.Crab.SetLine(fields[1], fields[1], Color.White, 1, 0.01, 2, DashStyle.Solid);
            this.TlsAd = base.Crab.AddLine(fields[2], panelID);
            base.IndPeriod[0].ParamValue = periods[0];
            base.IndPeriod[1].ParamValue = periods[1];
            base.IndPeriod[2].ParamValue = periods[2];
            base.IndPeriod[3].ParamValue = periods[3];
            base.SourceDic["High"] = source[0];
            base.SourceDic["Low"] = source[1];
            base.Crab.DataSource.Columns.Add(this.DmfField);
            base.Crab.DataSource.Columns.Add(this.DmzField);
            base.Crab.DataSource.Columns.Add(this.DmfSumField);
            base.Crab.DataSource.Columns.Add(this.DmzSumField);
        }

        public BarShape BsDdi
        {
            get
            {
                return this.bsDdi;
            }
            set
            {
                this.bsDdi = value;
            }
        }

        public string DmfField
        {
            get
            {
                return this.dmfField;
            }
            set
            {
                this.dmfField = value;
            }
        }

        public string DmfSumField
        {
            get
            {
                return this.dmfSumField;
            }
            set
            {
                this.dmfSumField = value;
            }
        }

        public string DmzField
        {
            get
            {
                return this.dmzField;
            }
            set
            {
                this.dmzField = value;
            }
        }

        public string DmzSumField
        {
            get
            {
                return this.dmzSumField;
            }
            set
            {
                this.dmzSumField = value;
            }
        }

        public LineShape TlsAd
        {
            get
            {
                return this.tlsAd;
            }
            set
            {
                this.tlsAd = value;
            }
        }

        public LineShape TlsAddi
        {
            get
            {
                return this.tlsAddi;
            }
            set
            {
                this.tlsAddi = value;
            }
        }
    }
}

